import pygame
from pygame.sprite import Sprite

class Ship(Sprite):
    """说明飞船的类"""
    def __init__(self, ai_game):
        """初始化飞船并设置其初始位置"""
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()

        # 导入飞船图像并读取其外接矩形
        self.image = pygame.image.load('picture/ship.bmp')
        self.rect = self.image.get_rect()

        # 确定飞船在屏幕上的出现位置
        self.rect.midbottom = self.screen_rect.midbottom

        # 在飞船的属性x中储存一个浮点数
        self.x = float(self.rect.x)

        # 移动标志
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """根据移动标志移动飞船"""
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.rect.x += self.settings.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.rect.x -= self.settings.ship_speed
        if self.moving_up and self.rect.y > 0:
            self.rect.y -= self.settings.ship_speed
        if self.moving_down and self.rect.y < 750:
            self.rect.y += self.settings.ship_speed

    def blitme(self):
        """在指定位置绘制飞船"""
        self.screen.blit(self.image, self.rect)






